package com.example.bookkeeping_book.utility;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.bookkeeping_book.R;
import com.example.bookkeeping_book.service.MainActivity;

import java.io.InputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MyDialog extends Dialog {
    private Button btnAdd;
    private Button btnCache;
    private TextView title;
    private EditText editName;
    private String addStr, titleStr, hintStr,editStr;
    private Integer input;
    private onAddOnclickListener addOnclickListener;//取消按钮被点击了的监听器

    public void setAddOnclickListener(String str, onAddOnclickListener onAddOnclickListener) {
        if (str != null) {
            addStr = str;
        }
        this.addOnclickListener = onAddOnclickListener;
    }

    public MyDialog(Context context) {
        super(context);
    }


    protected MyDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.my_dialog);
        init();
    }

    public String getEditText() {
        return editName.getText().toString();
    }

    private void init() {
        editName = findViewById(R.id.edt_name);
        btnAdd = findViewById(R.id.btn_add);
        btnCache = findViewById(R.id.btn_cache);
        title = findViewById(R.id.tv_title);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (addOnclickListener != null) {
                    addOnclickListener.onAddClick();
                }
            }
        });
        btnCache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        if (titleStr != null) {
            title.setText(titleStr);
        }

        if (addStr != null) {
            btnAdd.setText(addStr);
        }

        if (hintStr != null) {
            editName.setHint(hintStr);
        }

        if (input != null) {
            editName.setInputType(input);
        }

        if(editStr != null) {
            editName.setText(editStr);
        }
    }

    public interface onAddOnclickListener {
        public void onAddClick();
    }


    public void setTitle(String title) {
        titleStr = title;
    }

    public void setHint(String hint) {
        hintStr = hint;
    }

    public void setInput(int i) {
        input = i;
    }
    public void setEditStr(String edit) {
        editStr = edit;
    }
}
